
$(function() {
	$(".nav-ul li").eq(4).addClass("cur");
    if ($(".form-inline div").hasClass('form-more')) {
        $('.btn-more').show()
    }
    $('.btn-clear-form').click(function () {
        $('.form-inline input').val('')
    })
	 
	query(1);
	
	$('#pageNum').dropdown();
	$('#dropdown').on('shown.bs.dropdown', function () {
	     $('.dropdown-menu li').click(function () {
	         var str = $(this).children('a').html();
	         var size = $(this).children('a').children('label').text();
	         str += '<span class="caret"></span>';
	         $("#pageNum").html(str);
	         
	         query(1);
	     })
	 });
	
	
	var htmlSrc = {
		houseNum: 'ircsHouse',
		ipNum: 'ircsIp',
		totalBandWidth: 'ircsBandWidth',
		userNum: 'ircsUser',
		domainNum: 'ircsDomain',
		publicDomainNum: 'ircsPublicDomain',
    } 

    var iframeSrc = '';
    var iframeStr = '';
   /* $('#subDialog').on('show.bs.modal', function (e) {
        var btnId = e.relatedTarget.id;
        //console.log(btnId, "+++")
        if (btnId === 'houseNum') {
            iframeSrc = htmlSrc.houseNum;
            iframeStr = '<iframe src=' + iframeSrc + ' frameborder="0"></iframe>';
            $('.modal-body').html(iframeStr);
        } else if (btnId === 'ipNum') {
            iframeSrc = htmlSrc.ipNum;
            iframeStr = '<iframe src=' + iframeSrc + ' frameborder="0"></iframe>';
            $('.modal-body').html(iframeStr);
        } else if (btnId === 'totalBandWidth') {
            iframeSrc = htmlSrc.totalBandWidth;
            iframeStr = '<iframe src=' + iframeSrc + ' frameborder="0"></iframe>';
            $('.modal-body').html(iframeStr);
        } else if (btnId === 'userNum') {
            iframeSrc = htmlSrc.userNum;
            iframeStr = '<iframe src=' + iframeSrc + ' frameborder="0"></iframe>';
            $('.modal-body').html(iframeStr);
        } else if (btnId === 'domainNum') {
            iframeSrc = htmlSrc.domainNum;
            iframeStr = '<iframe src=' + iframeSrc + ' frameborder="0"></iframe>';
            $('.modal-body').html(iframeStr);
        } else {
            iframeSrc = htmlSrc.publicDomainNum;
            iframeStr = '<iframe src=' + iframeSrc + ' frameborder="0"></iframe>';
            $('.modal-body').html(iframeStr);
        }
    })
    $('#subDialog').on('hidden.bs.modal', function (e) {
        iframeSrc = ''
    });*/
});


function linkPage(id, ircsId) {
	var loginUser = $("#loginUser").val();
	var userLevel = $("#userLevel").val();
	var userId = $("#userId").val();
	var htmlSrc = {
		houseNum : 'ircsHouseLink?ircsId='+ircsId+'&userId='+userId+'&loginUser='+loginUser+'&userLevel='+userLevel,
		ipNum : 'ircsIpLink?ircsId='+ircsId+'&userId='+userId+'&loginUser='+loginUser+'&userLevel='+userLevel,
		totalBandWidth : 'ircsBandWidthLink?ircsId='+ircsId+'&userId='+userId+'&loginUser='+loginUser+'&userLevel='+userLevel,
		userNum : 'ircsUserLink?ircsId='+ircsId+'&userId='+userId+'&loginUser='+loginUser+'&userLevel='+userLevel,
		domainNum : 'ircsDomainLink?ircsId='+ircsId+'&userId='+userId+'&loginUser='+loginUser+'&userLevel='+userLevel,
		publicDomainNum : 'ircsPublicDomainLink?ircsId='+ircsId+'&userId='+userId+'&loginUser='+loginUser+'&userLevel='+userLevel
	}
	var iframeSrc = '';
	if (id == 'houseNum') {
		iframeSrc = htmlSrc.houseNum;
	}else if(id == 'ipNum'){
		iframeSrc = htmlSrc.ipNum;
	}else if(id == 'totalBandWidth'){
		iframeSrc = htmlSrc.totalBandWidth;
	}else if(id == 'userNum'){
		iframeSrc = htmlSrc.userNum;
	}else if(id == 'domainNum'){
		iframeSrc = htmlSrc.domainNum;
	}else if(id == 'publicDomainNum'){
		iframeSrc = htmlSrc.publicDomainNum;
	}
	var iframeStr = '<iframe src=' + iframeSrc+ ' frameborder="0" id="iframeId"></iframe>';
	$('#modalBody').html(iframeStr);
	/*var iframe = document.getElementById("iframeId");
    iframe.onload = function () {
        var myiframeH = Math.max(iframe.contentWindow.document.documentElement.scrollHeight,iframe.contentDocument.body.scrollHeight);
        console.log(myiframeH, "++++")
        iframe.height = myiframeH  
    }*/
}

function jumpQuery(){
	var page = $("#jump_page").val();
	var reg = /^\+?[1-9][0-9]*$/;//判断是否为正整数 
    var flag = reg.test(trim(page));
    if(!flag){
    	layer.alert("请输入正整数！", {
			icon : 3,
			offset: '100px'
		});
    }else{
    	var totalPages = $("#total_pages").text();
    	if(parseInt(page) > parseInt(totalPages)){
    		layer.alert("跳转页数不能超过总页数！", {
    			icon : 3,
    			offset: '100px'
    		});
    	}else{
    		query(page);
    	}
    }
}

function query(page){
	layer.load(4);
	$.ajax({
		type : "POST",
		url :  "ircsData/getIrcsDataInfo",
		data : {
			"currentPage" : page,
			"userId" : $("#userId").val(),
			"loginUser" : $("#loginUser").val(),
			"userLevel" : $("#userLevel").val(),
			"ircsId" : $("#ircsId").val(),
			"enterprise" : $("#enterprise").val(),
			"ircsName" : $("#ircsName").val(),
			"pageSize" : $("#pageNum").children('strong').text()
		},
		dataType : "json",
		success : function(data) {
			layer.closeAll();
			if (data.flag) {
				var tread = data.datas;
				insertInfoTpl(tread, $('#tread'), $('#treadTpl'));
				var currentPage = data.currentPage;
				var totalPages = data.totalPages;
				var totalData = data.totalData;
				$("#total_data").text(totalData);
				$("#total_pages").text(totalPages);
				var options = {
					currentPage : currentPage, // 当前的请求页面。
					totalPages : totalPages, // 一共多少页。
					size : "normal", // 应该是页眉的大小。
					bootstrapMajorVersion : 3, // bootstrap的版本要求。
					alignment : "right",
					numberOfPages : 5, // 一页显示几个按钮
					itemTexts : function(type, page, current) { // 如下的代码是将页眉显示的中文显示我们自定义的中文。
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						case "page":
							return page;
						}
					},
					tooltipTitles : function(type, page, current) { // 提示框，当鼠标放上去给提示
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						}
					},
					onPageClicked : function(event, originalEvent, type, page) {
						layer.load(4);
						$.ajax({
							type : "POST",
							url : "ircsData/getIrcsDataInfo",
							data : {
								"currentPage" : page,
								"userId" : $("#userId").val(),
								"loginUser" : $("#loginUser").val(),
								"userLevel" : $("#userLevel").val(),
								"ircsId" : $("#ircsId").val(),
								"enterprise" : $("#enterprise").val(),
								"ircsName" : $("#ircsName").val(),
								"pageSize" : $("#pageNum").children('strong').text()
							},
							dataType : "json",
							success : function(data) {
								if (data.flag) {
									layer.closeAll();
									var tread = data.datas;
									insertInfoTpl(tread, $('#tread'),$('#treadTpl'));
								}
							},
							error : function(data) {
								layer.closeAll();
							}
						});
					}
				}
				$('#pageLimit').bootstrapPaginator(options);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

/**
 * [insertInfoTpl 插入 html信息]
 * @param  {[type]} data [渲染所需的数据]
 * @param  {[type]} dom  [加入页面中的哪个DOM节点]
 * @return {[type]}      [description]
 */
function insertInfoTpl(data, dom, tpl) {
	dom.empty();
    var infoTpl = tpl.html()
    var compiled = _.template(infoTpl)
    var html = ''
    _.forEach(data, function (item, index) {
        item.index = index + 1
        html += compiled(item)
    })
    dom.append($(html))
}

function detailInfo(ircsId){
	layer.load(4);
	$.ajax({
		type : "POST",
		url : "ircsData/getDetailInfo",
		data : {
			"ircsId" : ircsId
		},
		dataType : "json",
		success : function(data) {
			if (data.flag) {
				layer.closeAll();
				var datas = data.datas;
				
				var detail = $("#detail_info");
				detail.empty();
				
				
			    var str  =  '<h4 class="table-title">详细信息</h4>'+
						    '<div class="table-responsive">'+
						    '	<table class="table table-bordered">'+
						    '		<thead><tr><th width="110"></th><th width="110"></th><th width="110"></th><th width="110"></th></tr></thead>'+
						    '		<tbody>'+
						    '			<tr><td colspan="4">企业信息</td></tr>'+
						    '			<tr><td>企业名称：</td><td colspan="3">'+filter(datas.ircsName)+'</td></tr>'+
						    '			<tr><td>企业法人：</td><td colspan="3">'+filter(datas.corp)+'</td></tr>'+
						    '			<tr><td>经营许可证：</td><td colspan="3">'+filter(datas.ircsId)+'</td></tr>'+
						    '			<tr><td>地址：</td><td colspan="3">'+filter(datas.ircsAdd)+'</td></tr>'+
						    '			<tr class="title"><td colspan="2">应急责任人信息</td><td colspan="2">安全责任人信息</td></tr>'+
						    '			<tr><td>责任人姓名：</td><td>'+filter(datas.name)+'</td><td>责任人姓名：</td><td>'+filter(datas.houseOfficeName)+'</td></tr>'+
						    '			<tr><td>证件类型：</td><td>'+getIdType(datas.idType)+'</td><td>证件类型：</td><td>'+getIdType(datas.houseOfficeIdType)+'</td></tr>'+
						    '			<tr><td>证件号码：</td><td>'+filter(datas.id)+'</td><td>证件号码：</td><td>'+filter(datas.houseOfficeId)+'</td></tr>'+
						    '			<tr><td>固定电话：</td><td>'+filter(datas.tel)+'</td><td>固定电话：</td><td>'+filter(datas.houseOfficeTel)+'</td></tr>'+
						    '			<tr><td>手机：</td><td>'+filter(datas.mobile)+'</td><td>手机：</td><td>'+filter(datas.houseOfficeMobile)+'</td></tr>'+
						    '			<tr><td>Email：</td><td>'+filter(datas.email)+'</td><td>Email：</td><td>'+filter(datas.houseOfficeEmail)+'</td></tr>'+
						    '		</tbody>'+
						    '	</table>'+
						    '</div>'  
				detail.append(str);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

function getIdType(idType){
	var idType = filter(idType);
	if(idType == 1){
		idType = "工商营业执照号码";
	}else if(idType == 2){
		idType = "身份证";
	}else if(idType == 3){
		idType = "组织机构代码证书";
	}else if(idType == 4){
		idType = "事业法人证书";
	}else if(idType == 5){
		idType = "军队代号";
	}else if(idType == 6){
		idType = "社团法人证书";
	}else if(idType == 7){
		idType = "护照";
	}else if(idType == 8){
		idType = "军官证";
	}else if(idType == 9){
		idType = "台胞证";
	}else if(idType == 999){
		idType = "其他";
	}
	return idType;
}

function filter(name){
	if(name == null || name == "null" || name == undefined){
		return "";
	}else{
		return name
	}
}

function trim(str){ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
